package com.itwn.controller;

import com.github.pagehelper.PageInfo;
import com.itwn.entity.Role;
import com.itwn.result.ResponseResult;
import com.itwn.service.RoleService;
import com.itwn.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @topic:
 **/
@RequestMapping("role")
@RestController
@Api(value = "角色管理")
@CrossOrigin
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping("/list")
    @ApiOperation("查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", required = false, dataType = "Integer", paramType = "query")
    })
    public ResponseResult<PageInfo> findAll(String roleName,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "3") Integer pageSize) {
        PageInfo<Role> pageInfo = roleService.findAll(roleName, pageNum, pageSize);
        ResponseResult<PageInfo> result = new ResponseResult<>();
        if (pageInfo != null) {
            result.setCode(200);
            result.setData(pageInfo);
            result.setMsg("查询成功");
        } else {
            result.setCode(103);
            result.setMsg("查询失败");
        }
        return result;
    }

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加角色")
    public ResponseResult addRole(@RequestBody RoleVo roleVo) {

        //对选中的权限ID去重
        List<Integer> perList = roleVo.getPerList();
        Set<Integer> set = new HashSet<>();
        for (Integer integer : perList) {
            set.add(integer);
        }
        List<Integer> permissionIds = new ArrayList<>();
        for (Integer integer : set) {
            permissionIds.add(integer);
        }
        roleVo.setPerList(permissionIds);

        Date date = new Date();
        roleVo.setInsertTime(date);
        roleVo.setUpdateTime(date);
        int i = roleService.addRole(roleVo);
        ResponseResult<RoleVo> result = new ResponseResult<>();
        if (i == 1) {
            result.setCode(200);
            result.setMsg("添加成功");
            result.setData(roleVo);
        } else {
            result.setCode(110);
            result.setMsg("添加失败");
        }

        return result;
    }

    @GetMapping("/findRoleVo")
    @ApiOperation(value = "查询角色及所其拥有的权限")
    public ResponseResult<RoleVo> findRoleVo(Integer roleId) {
        RoleVo roleVo = roleService.findRoleVo(roleId);
        ResponseResult<RoleVo> result = new ResponseResult<>();
        if (roleVo != null) {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(roleVo);
        } else {
            result.setCode(107);
            result.setMsg("查询失败");
        }
        return result;
    }

    @DeleteMapping("/remove/{roleId}")
    @ApiOperation(value = "删除角色的方法")
    public ResponseResult removeRole(@PathVariable(value = "roleId") Integer roleId) {
        int i = roleService.removeRoleAndPermission(roleId);
        ResponseResult<Object> result = new ResponseResult<>();
        if (i == 1) {
            result.setCode(200);
            result.setMsg("删除成功");
        } else {
            result.setCode(109);
            result.setMsg("删除失败");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "更新角色")
    public ResponseResult<RoleVo> modifyRoleVo(@RequestBody RoleVo roleVo) {

        //对选中的权限ID去重
        List<Integer> perList = roleVo.getPerList();
        Set<Integer> set = new HashSet<>();
        for (Integer integer : perList) {
            set.add(integer);
        }
        List<Integer> permissionIds = new ArrayList<>();
        for (Integer integer : set) {
            permissionIds.add(integer);
        }
        roleVo.setPerList(permissionIds);

        roleVo.setUpdateTime(new Date());
        int i = roleService.modifyRoveAndPermission(roleVo);
        ResponseResult<RoleVo> result = new ResponseResult<>();
        if (i == 1) {
            result.setCode(200);
            result.setMsg("更新成功");
            result.setData(roleVo);
        } else {
            result.setCode(108);
            result.setMsg("更新失败");
        }
        return result;

    }
}